package com.hongkang.springtrains.web;


import com.hongkang.springtrains.model.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.converter.cbor.MappingJackson2CborHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class RestControllerTestWithRestTemplate {

    private RestTemplate restTemplate ;


    @Before
    public void setup(){

        restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().add(new MappingJackson2CborHttpMessageConverter());

    }

    @Test
    public void printUser(){
        Map<String,String> params = new HashMap<>();
        params.put("name","Do");
        params.put("email","dujiang@sohu.com");
        String url = "http://localhost:8080/users/tom/{name}/{email}";
        User result = restTemplate.getForObject(url,User.class,params);
        System.out.println(result);
    }



    @Test
    public void printUser2(){
        Map<String,String> params = new HashMap<>();
        params.put("name","Do");
        params.put("email","dujiang@sohu.com");
        String url = "http://localhost:8080/users/tom/{name}/{email}";
        HttpHeaders requestHeaders = new HttpHeaders();
        //requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
        requestHeaders.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        requestHeaders.set("MyHeader","MyValue");
        HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);


        HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET,requestEntity,String.class,params);
        System.out.println(response.getHeaders());
        System.out.println(response.getBody());
       // User result = restTemplate.getForObject(url,User.class,params);
       // System.out.println(result);
    }





}
